const fileService = require('../service/file.service')
const {APP_HOST, APP_PORT} = require('../app/config')
const userService = require('../service/user.service')

class FileController {
  async createAvatar(ctx, next) {
    const {mimetype, filename, size} = ctx.req.file
    const {id} = ctx.user
    const result = await fileService.createAvatar(mimetype, filename, size, id)
    console.log(result)
    const avatarURL = `${APP_HOST}:${APP_PORT}/users/${id}/avatar`
    await userService.updateAvatarUrl(avatarURL, id)
    ctx.body = '头像上传成功'
  }

  async savePictureInfo(ctx, next) {
    const files = ctx.req.files
    const {id} = ctx.user
    const {momentId} = ctx.query
    // console.log(id, momentId)
    for (const file of files) {
      const {filename, mimetype, size} = file
      await fileService.createFiles(filename, mimetype, size, id, momentId)
    }
  }
}

module.exports = new FileController()